import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { PermissionService } from './permission.service';
import { CreatePermissionDto } from './dto/create-permission.dto';
import { UpdatePermissionDto } from './dto/update-permission.dto';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';

@ApiTags('权限管理')
@Controller('permission')
export class PermissionController {
  constructor(private readonly permissionService: PermissionService) {}

  @ApiOperation({ summary: '新增权限' })
  @Post()
  create(@Body() createPermissionDto: CreatePermissionDto) {
    return this.permissionService.create(createPermissionDto);
  }

  @ApiOperation({ summary: '查询所有权限' })
  @Get()
  findAll(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
  ) {
    return this.permissionService.findAll({ page, pageSize });
  }

    @ApiOperation({ summary: '分页查询权限列表' })
    @ApiQuery({
      name: 'page',
      type: Number,
      description: '当前页码',
      required: true,
      example: 1,
    })
    @ApiQuery({
      name: 'pageSize',
      type: Number,
      description: '分页大小',
      required: true,
      example: 10,
    })
    @ApiQuery({
      name: 'name',
      type: String,
      description: '权限名称',
      required: false,
      example: '',
    })
    @ApiQuery({
      name: 'code',
      type: String,
      description: '权限编码',
      required: false,
      example: '',
    })
    @Get('page')
    pageSearch(
      @Query('page', new ParseIntPipe()) page = 1,
      @Query('pageSize', new ParseIntPipe()) pageSize = 10,
      @Query('name') name?: string,
      @Query('code') code?: string,
    ) {
      return this.permissionService.pageSearch({
        page,
        pageSize,
        name,
        code,
      });
    }

  @ApiOperation({ summary: '查询权限详情' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.permissionService.findOne(id);
  }

  @ApiOperation({ summary: '更新权限信息' })
  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updatePermissionDto: UpdatePermissionDto,
  ) {
    return this.permissionService.update(id, updatePermissionDto);
  }

  @ApiOperation({ summary: '删除权限' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.permissionService.remove(id);
  }
}
